﻿using System;
using Token.Piker.Common;
using System.Web.Security;
using Token.Piker.BLL;

namespace Token.Piker.Web
{
    public partial class Register : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnReg_Click(object sender, EventArgs e)
        {
            if (Session["UserValidateCode"].ToString().ToUpper() != txtValidateCode.Text.Trim().ToUpper())
            {
                MessageBox.Show(this.Page, "验证码错误");
                return;
            }

            Token.Piker.Model.User user = new Model.User();
            user.UserName = txtUserName.Text.Trim();
            string password = FormsAuthentication.HashPasswordForStoringInConfigFile(txtPassword.Text.Trim(), "MD5");
            user.UserPwd = password;
            DateTime now = DateTime.Now;
            user.RegisterTime = now;
            user.LastLogTime = now;
            user.Flag = 0;

            UserBLL bll = new UserBLL();

            try
            {
                bll.Add(user);
                MessageBox.Show(this.Page, "注册成功！");
                Session[GlobalConstants.PIKER_USER_ID] = user.ID;
                Session[GlobalConstants.PIKER_USER_NAME] = user.UserName;
                Response.Redirect("~/Default.aspx");
            }
            catch
            {
                MessageBox.Show(this.Page, "注册失败，请稍后重试！");
            }
        }
    }
}